﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using Models;
using Common;

namespace BLL
{
    public class ProjectMainManager
    {

        private ProjectMainService objProjectMainService = new ProjectMainService();
        private WorkflowService objWorkflowService = new WorkflowService();
        private Normal objNormal = new Normal();
        #region 新增更改
        /// <summary>
        /// 添加或修改基础数据临时表
        /// </summary>
        /// <param name="PJMain"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public string InsertOrUpdateTempProjectInfo(ProjectMain PJMain, string tableName)
        {
            int count = objProjectMainService.GetProjectInfoOfWorkParentId(PJMain.WorkParentId, null, "PJMainsTemp");
            if (count == 1)
            {
                objProjectMainService.UpdateProjectInfo(PJMain, tableName);
            }
            else
            {
                objProjectMainService.InsertProjectInfo(PJMain, tableName);
            }
            return PJMain.PJId;
        }

        /// <summary>
        /// 添加数据到项目基础数据主表
        /// </summary>
        /// <param name="PJMain"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public string InsertMasterProjectInfo(ProjectMain PJMain, string tableName)
        {
            objProjectMainService.InsertProjectInfo(PJMain, tableName);
            return PJMain.PJId;
        }

        /// <summary>
        /// 更改数据到项目基础数据主表
        /// </summary>
        /// <param name="PJMain"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public string UpdateMasterProjectInfo(ProjectMain PJMain, string tableName)
        {
            objProjectMainService.UpdateProjectInfo(PJMain, tableName);
            return PJMain.PJId;
        }
        /// <summary>
        /// 根据项目号更改项目主表状态
        /// </summary>
        /// <param name="pjId"></param>
        /// <param name="afterStatues"></param>
        /// <returns></returns>
        public int UpdateProjectInfo(int workParentId, string afterStatues,string tableName)
        {
            return objProjectMainService.UpdateProjectInfo(workParentId, afterStatues, tableName);
        }
        /// <summary>
        /// 根据工作流号查询主表信息
        /// </summary>
        /// <param name="pjId"></param>
        /// <returns></returns>
        public ProjectMain GetProjectInfo(int workParentId,string pjId, string tablename)
        {
            ProjectMain objProjectMain = new ProjectMain();
            if (tablename == "PJMains") { objProjectMain = objProjectMainService.GetProjectInfo(workParentId, null, tablename); }
            else
            {
                objProjectMain = objProjectMainService.GetProjectInfo(workParentId, null, tablename);
                if (objProjectMain == null)
                {
                    objProjectMain = objProjectMainService.GetProjectInfo(-1,pjId, "PJMains");
                    if(objProjectMain!=null)
                    {
                        objProjectMain.Statues = "0";
                    }
                }
            }
            return objProjectMain;
        }
        /// <summary>
        /// 各阶段实际完成时间（临时表）
        /// </summary>
        /// <param name="PJMain"></param>
        /// <returns></returns>
        public int InsertDateTemp(ProjectMain PJMain)
        {
            return objProjectMainService.InsertDateTemp(PJMain);
        }
        /// <summary>
        /// 更新实际完成时间
        /// </summary>
        /// <param name="pjId"></param>
        /// <param name="endDate"></param>
        /// <param name="stageName"></param>
        /// <returns></returns>
        public int UpdateStage(string pjId, DateTime endDate, string stageName)
        {
            return objProjectMainService.UpdateStage(pjId, endDate, stageName);
        }
        /// <summary>
        /// 更新实际完成时间(临时表)
        /// </summary>
        /// <param name="PJMain"></param>
        /// <returns></returns>
        public int UpdateStage(ProjectMain PJMain)
        {
            return objProjectMainService.UpdateStage(PJMain);
        }
        /// <summary>
        /// 更新DateTemp状态
        /// </summary>
        /// <param name="pjId"></param>
        /// <param name="stageName"></param>
        /// <returns></returns>
        public int UpdateStatues(string pjId, string stageName)
        {
            return objProjectMainService.UpdateStatues(pjId, stageName);
        }
        #endregion
        /// <summary>
        /// 获取项目号的创建者
        /// </summary>
        /// <param name="pjId"></param>
        /// <returns></returns>
        public string GetUserName(string pjId)
        {
            return objProjectMainService.GetUserName(pjId);
        }
        /// <summary>
        /// 获取最大Id行的Number值,根据规则生成新Number值
        /// </summary>
        /// <returns></returns>
        public string GetNewNumber()
        {
            string result = "";
            string number = objProjectMainService.GetProjectLastRow();
            result = objNormal.NewNumber(number, "PJ");
            return result;
        }
        /// <summary>
        /// 查询项目主表中是否有工作流号
        /// </summary>
        /// <param name="pjId"></param>
        /// <param name="statues"></param>
        /// <returns></returns>
        public int GetProjectInfoOfWorkParentId(int workParentId, string pjId, string tableName)
        {
            return objProjectMainService.GetProjectInfoOfWorkParentId(workParentId, pjId, tableName);
        }
        /// <summary>
        /// 查询DateTemp记录
        /// </summary>
        /// <param name="workParentId"></param>
        /// <returns></returns>
        public ProjectMain GetDateTemp(int workParentId)
        {
            return objProjectMainService.GetDateTemp(workParentId);
        }
        /// <summary>
        /// 查询是否有重复值
        /// </summary>
        /// <param name="pjId"></param>
        /// <param name="stageName"></param>
        /// <returns></returns>
        public int GetCount(string pjId, string stageName,string statues)
        {
            return objProjectMainService.GetCount(pjId, stageName,statues);
        }
        /// <summary>
        /// 查询是否有PJ号(模具号)
        /// </summary>
        /// <param name="pjId"></param>
        /// <param name="stageName"></param>
        /// <returns></returns>
        public int GetCount(string pjId)
        {
            List<int> list = new List<int>();
            int result=0;
            list = objProjectMainService.GetCount(pjId);
            if(list.Count>=1)
            {
                foreach (var item in list)
                {
                    if(objWorkflowService.GetWorkflowStatusDefine(item)>0)
                    {
                        result = 1;
                        break;
                    }
                }
            }
            return result;
        }
    }
}
